#!/usr/bin/env python
#coding=utf-8
#File: directoryStatWalker.py

import os
import stat

class DirectoryStatWalker:
    # a forward iterator that traverses a directory tree, and
    # returns the filename and additional file information
    
    def __init__(self, directory):
        self.stack = [directory]
        self.files = []
        self.index = 0
        self.directory = ""
        
    def __getitem__(self, index):
        while 1:
            try:
                file = self.files[self.index]
                self.index = self.index + 1
            except IndexError:
                self.directory = sel.stack.pop()
                self.files = os.listdir(self.directory)
                self.index = 0
            else:
                # got a filename
                fullname = os.path.join(self.directory, file)
                st = os.stat(fullname)
                mode = st[stat.ST_MODE]
                if stat.S_ISDIR(mode) and not stat.S_ISLNK(mode):
                    self.stack.append(fullname)
                return fullname, st
            
        if __name__ == "__main__":
            for file, st in DirectoryStatWalker("."):
                print file, st[stat.ST_SIZE]
                
                
                